---
title: "conjoint AMCE"
author: "Dai Yamao"
date: "`r format(Sys.time(), '%Y-%m-%d')`"
output:
  html_document: default
  pdf_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.width = 10, fig.height = 5)
rm(list=ls())

require(cjoint)
require(tidyverse)

dat_cjoint <- read_csv("data/conjoint_data_tr.csv")
dat_cjoint[,c(18:22)] <- data.frame(lapply(dat_cjoint[,c(18:22)],as.factor))

dat_cjoint <- dat_cjoint %>% 
  mutate(patronage = case_when(Q8 <= 2 ~ "high",
                               Q8 == 4 | Q8 == 5 ~ "low",
                               Q8 == 3 | Q8 == 6 ~ "other"))
dat_cjoint$patronage <- as.factor(dat_cjoint$patronage)

dat_cjoint <- dat_cjoint %>% 
  mutate(sunni = ifelse(Sect == 1, 1, 0),
         shia = ifelse(Sect == 2, 1, 0),
         maronite = ifelse(Sect == 3, 1, 0),
         druze = ifelse(Sect == 4, 1, 0))

dat_cjoint$Sect <- as.factor(dat_cjoint$Sect)

```

# conjoint model: whole
```{r}
str(dat_cjoint)

attr_list <- list()             
attr_list[["constituent"]] <- c("Political party", "Za'im", "Business people", "Civil society organizations")
attr_list[["sect"]] <- c("Sunni", "Shia", "Maronite", "Druze")
attr_list[["domestic"]] <- c("Anti sectarianism", "Autonomy", "Border control", "Equal representation", 
                           "Effective central gov", "Census", "Equal development", 
                           "Sectarian base", "Corruption")
attr_list[["foreign"]] <- c("Palestine", "Syria", "Iran", "Saudi", "Liberation of south") 


cjoint_design <- makeDesign(type = "constraints",
                            attribute.levels = attr_list)
cjoint_pool <- amce(choice ~ constituent + sect + domestic + foreign,
                    data = dat_cjoint,
                    respondent.id = "id",
                    cluster = TRUE,
                    design = cjoint_design,
                    na.ignore = TRUE)
plot(cjoint_pool)
summary(cjoint_pool)
```

# subset: patronage
```{r}

reg01 <- amce(choice ~ constituent + sect + domestic + foreign,
                    data = subset(dat_cjoint, patronage = "high"),
                    respondent.id = "id",
                    cluster = TRUE,
                    design = cjoint_design,
                    na.ignore = TRUE)
plot(reg01)
summary(reg01)

reg02 <- amce(choice ~ constituent*patronage + sect*patronage + domestic*patronage + foreign*patronage,
                    data = dat_cjoint,
                    respondent.id = "id",
                    cluster = TRUE,
                    na.ignore = TRUE)
plot(reg02, facet.names = c("patronage"), show.all = TRUE)
summary(reg02)

```

# subset: sect
```{r, fig.height = 15, dpi = 300}

reg07 <- amce(choice ~ constituent*Sect + sect*Sect + domestic*Sect + foreign*Sect,
                    data = dat_cjoint,
                    respondent.id = "id",
                    cluster = TRUE,
                    na.ignore = TRUE)
plot(reg07, facet.names = c("Sect"), show.all = TRUE)
summary(reg07)
```

# subset: sect
```{r}
# sunni
reg03 <- amce(choice ~ constituent + sect + domestic + foreign,
                    data = subset(dat_cjoint, sunni = "1"),
                    respondent.id = "id",
                    cluster = TRUE,
                    design = cjoint_design,
                    na.ignore = TRUE)
plot(reg03)
summary(reg03)


dat_sunni <- dat_cjoint %>% 
  filter(Sect == "1") 

reg_sunni <- amce(choice ~ constituent + sect + domestic + foreign,
                    data = dat_sunni,
                    respondent.id = "id",
                    cluster = TRUE,
                    design = cjoint_design,
                    na.ignore = TRUE)
plot(reg_sunni)
summary(reg_sunni)

# shia
reg04 <- amce(choice ~ constituent + sect + domestic + foreign,
                    data = subset(dat_cjoint, shia = "1"),
                    respondent.id = "id",
                    cluster = TRUE,
                    design = cjoint_design,
                    na.ignore = TRUE)
plot(reg04)
summary(reg04)

# maronite
reg05 <- amce(choice ~ constituent + sect + domestic + foreign,
                    data = subset(dat_cjoint, maronite = "1"),
                    respondent.id = "id",
                    cluster = TRUE,
                    design = cjoint_design,
                    na.ignore = TRUE)
plot(reg05)
summary(reg05)

dat_maronite <- dat_cjoint %>% 
  filter(Sect == "3") 

reg_maronite <- amce(choice ~ constituent + sect + domestic + foreign,
                    data = dat_maronite,
                    respondent.id = "id",
                    cluster = TRUE,
                    design = cjoint_design,
                    na.ignore = TRUE)
plot(reg_maronite)
summary(reg_maronite)

# duruze
reg06 <- amce(choice ~ constituent + sect + domestic + foreign,
                    data = subset(dat_cjoint, druze = "1"),
                    respondent.id = "id",
                    cluster = TRUE,
                    design = cjoint_design,
                    na.ignore = TRUE)
plot(reg06)
summary(reg06)

```